﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX.UI
{
    public class LevelSelector : Window
    {
        Editor LevelToGo;
        Editor Passcode;
        Text LabelLevel;
        Text LabelPass;

        public LevelSelector(MainGame game, Window parent)
            : base(game, parent)
        {
            Rectangle rect = game.GameRectangle;
            int width = rect.Width;
            int height = rect.Height;
            width = width / 3;
            height = height * 2 / 5;
            int left = rect.Left + (rect.Width - width) / 2;
            int top = rect.Top + (rect.Height - height) / 2;
            this.SetRectangle(new Rectangle(left, top, width, height));

            LabelLevel = new Text(game);
            LabelLevel.Value = "Level to Go: ";
            LabelPass = new Text(game);
            LabelPass.Value = "Passcode: ";

            LabelLevel.Align(this.boundingRect, AlignType.TopCenter, 0, 20);
            LabelPass.Align(this.boundingRect, AlignType.TopCenter, 0, 110);

            LevelToGo = new Editor(game);
            Passcode = new Editor(game);
            Vector2 size = new Vector2(width * 2 / 3, width / 15);
            LevelToGo.Align(this.boundingRect, AlignType.TopCenter, size, 0, 60);
            Passcode.Align(this.boundingRect, AlignType.TopCenter, size, 0, 150);

            this.AddChild(LevelToGo);
            this.AddChild(Passcode);
            this.AddChild(LabelLevel);
            this.AddChild(LabelPass);

            LevelToGo.Focus();
        }

        public override void TryHandleKeyEvent(GameTime gameTime, Window.KeyStrokeEvent e)
        {
            if (e.key == Keys.Enter)
            {
                // check password

                int levelNumber = 1;

                MainGame mg = (MainGame)Game;

                try
                {
                    levelNumber = int.Parse(LevelToGo.Value);
                }
                catch (Exception)
                {
                    MessageDialog md = new MessageDialog(mg, this, "Not a Valid Level.");
                    md.Show();
                    return;
                }

                String passwdInput = Passcode.Value;
                LevelBuilder lb = mg.LevelManager.GetLevelBuilder(levelNumber);
                if (lb == null)
                {
                    MessageDialog md = new MessageDialog(mg, this, "Not a Valid Level.");
                    md.Show();
                    return;
                }

                bool approved = lb.CheckPassword(passwdInput);
                if (approved)
                {
                    Game.RootWindow.CloseAllChildren();
                    Game._levelTransition = new LevelTransition(Game, null, lb,
                        new TimeSpan(0, 0, 3));
                    Game._levelTransition.Start();
                }
                else
                {
                    MessageDialog md = new MessageDialog(mg, this, "Wrong Passcode");
                    md.Show();
                    return;
                }
            }
            if (e.key == Keys.Tab)
            {
                if (LevelToGo.focused)
                {
                    Passcode.Focus();
                }
                else
                {
                    LevelToGo.Focus();
                }
            }
            else if (e.key == Keys.Escape)
            {
                this.Close();
            }
            e.consumed = true;
        }

        public override void Update(GameTime gameTime)
        {
            // as a container it's always focused
            focused = true;
            base.Update(gameTime);
        }
    }
}
